<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'orther:ven:pmContractor:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsOrtherVenPmContractorForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PmContractor, pmContractorSave, pmContractorForm } from '/@/api/orther/ven/pmContractor';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('orther.ven.pmContractor');
  const { showMessage } = useMessage();
  const record = ref<PmContractor>({} as PmContractor);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增承包商档案') : t('编辑承包商档案'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('编号'),
      field: 'contractorno',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      required: true,
    },
    {
      label: t('名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      required: true,
    },
    {
      label: t('简称'),
      field: 'contractorabbname',
      component: 'Input',
      componentProps: {
        maxlength: 500,
      },
      required: true,
    },
    {
      label: t('类型'),
      field: 'contractortype',
      component: 'Select',
      componentProps: {
        dictType: 'zf_ven_type',
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('联系人'),
      field: 'contacts',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('项目负责人'),
      field: 'cmanager',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('电话'),
      field: 'phone',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      required: true,
    },
    // {
    //   label: t('邮箱'),
    //   field: 'email',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 50,
    //   },
    // },
    // {
    //   label: t('传真'),
    //   field: 'fax',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 50,
    //   },
    // },
    // {
    //   label: t('网址'),
    //   field: 'webaddress',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 500,
    //   },
    // },
    {
      label: t('地址'),
      field: 'address',
      component: 'Input',
      componentProps: {
        maxlength: 500,
      },
    },
    {
      label: t('承包商简介'),
      field: 'contractorsyn',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 2000,
        rows: 6,
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmContractorForm(data);
    record.value = (res.pmContractor || {}) as PmContractor;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmContractorSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
